package com.lyl.servicevod.service;

import com.aliyun.oss.ClientException;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.lyl.exception.ZxjyException;
import com.lyl.servicevod.utils.AliyunVodSDKUtils;
import com.lyl.servicevod.utils.ConstantVodPropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Override
    public boolean removeVideoList(List<String> videoIdList) {
        try {
            //初始化
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ConstantVodPropertiesUtil.ACCESS_KEY_ID,
                    ConstantVodPropertiesUtil.ACCESS_KEY_SECRET);

            //创建请求对象
            //一次只能批量删20个
            String str = org.apache.commons.lang.StringUtils.join(videoIdList.toArray(), ",");
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(str);

            //获取响应
            DeleteVideoResponse response = client.getAcsResponse(request);

            System.out.print("RequestId = " + response.getRequestId() + "\n");

            return  true;
        } catch (ClientException e) {
            throw new ZxjyException(20001, "视频删除失败");
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ClientException e) {

        }
        return false;
    }



    @Override
    public String getUrlByVid(String videoId) {
        String url= null;
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantVodPropertiesUtil.ACCESS_KEY_ID, ConstantVodPropertiesUtil.ACCESS_KEY_SECRET);
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        try {

            GetPlayInfoRequest request = new GetPlayInfoRequest();
//            request.setSysEndpoint("oss-cn-shenzhen.aliyuncs.com");
            request.setVideoId(videoId);
            response=  client.getAcsResponse(request);

            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址

            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                url=playInfo.getPlayURL();
                System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
            }
            //Base信息
            System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
        return  url;
    }

    @Override
    public String uploadVideo(MultipartFile file) {

        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            // 视频标题
            String title =              originalFilename.substring(0, originalFilename.lastIndexOf("."));

            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantVodPropertiesUtil.ACCESS_KEY_ID,
                    ConstantVodPropertiesUtil.ACCESS_KEY_SECRET,
                    title, originalFilename, inputStream);


            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。
            // 其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            String videoId = response.getVideoId();
            if (!response.isSuccess()) {
                String errorMessage = "阿里云上传错误：" + "code：" + response.getCode() + ", message：" + response.getMessage();
                if(StringUtils.isEmpty(videoId)){
                    throw new ZxjyException(20001, errorMessage);
                }
            }

            return videoId;
        } catch (IOException e) {
            e.printStackTrace();
            throw new ZxjyException(20001, "zxjy vod 服务上传失败");
        }
    }

    @Override
    public boolean removeVideoById(String id) {
        try {
            //初始化
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ConstantVodPropertiesUtil.ACCESS_KEY_ID,
                    ConstantVodPropertiesUtil.ACCESS_KEY_SECRET);

            //创建请求对象
            //一次只能批量删20个
            String str =id+"";
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(str);

            //获取响应
            DeleteVideoResponse response = client.getAcsResponse(request);

            System.out.print("RequestId = " + response.getRequestId() + "\n");

            return  true;
        } catch (ClientException e) {
            throw new ZxjyException(20001, "视频删除失败");
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
